﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.Mvc;
using StartMVC.Models;
using PagedList;


namespace StartMVC.Controllers
{
    public class HomeController : BaseController
    {


        //首页
        public ActionResult Index()
        {
            var data = db.ProductCategorys.ToList();
            if (data.Count == 0)
            {
                db.ProductCategorys.Add(new ProductCategory() { Id = 1, Name = "文具" });
                db.ProductCategorys.Add(new ProductCategory() { Id = 2, Name = "礼品" });
                db.ProductCategorys.Add(new ProductCategory() { Id = 3, Name = "书籍" });
                db.ProductCategorys.Add(new ProductCategory() { Id = 4, Name = "用品" });
                db.SaveChanges();
                data = db.ProductCategorys.ToList();
            }
            return View(data);

            //var data = new List<ProductCategory>()
            //{
            //    new ProductCategory(){Id=1,Name="文具"},
            //    new ProductCategory(){Id=2,Name="礼品"},
            //    new ProductCategory(){Id=3,Name="书籍"},
            //    new ProductCategory(){Id=4,Name="用品"}
            //};
            //return View(data);
        }
        //商品列表
        public ActionResult ProductListOld(int id)
        {

            var productCategory = db.ProductCategorys.Find(id);
            if (productCategory != null)
            {
                var data = productCategory.Products.ToList();
                for (int i = 0; i < 10;i++ )
                {
                    data.Add(new Product() { Name = productCategory.Name + "类别下的商品1", Color = Color.Red, Description = "N\\A", Price = 99, PublishOn = DateTime.Now, ProductCategory = productCategory });
                }

                if (data.Count == 0)
                {
                    productCategory.Products.Add(new Product() { Name = productCategory.Name + "类别下的商品1", Color = Color.Red, Description = "N\\A", Price = 99, PublishOn = DateTime.Now, ProductCategory = productCategory });
                    productCategory.Products.Add(new Product() { Name = productCategory.Name + "类别下的商品2", Color = Color.Blue, Description = "N\\A", Price = 150, PublishOn = DateTime.Now, ProductCategory = productCategory });
                    db.SaveChanges();
                    data = productCategory.Products.ToList();
                }
                var pagedData = data.ToPagedList(pageNumber: 1, pageSize: 2);
                return View(pagedData);

            }
            else
                return HttpNotFound();

            // return View();
            //var productCategory = new ProductCategory() { Id = id, Name = "类别" + id };
            //var data = new List<Product>()
            //{
            //    new Product(){
            //        Id=1,
            //        ProductCategory=productCategory,
            //        Name="原子笔",
            //        Description="N/A",
            //        Price=30,
            //        PublishOn=DateTime.Now,
            //        Color=Color.Black
            //    },
            //    new Product()
            //    {
            //        Id=1,
            //        ProductCategory=productCategory,
            //        Name="铅笔",
            //        Description="N/A",
            //         Price=30,
            //         PublishOn=DateTime.Now,
            //         Color=Color.Black
            //    }
            //};
            //return View(data);
        }


        public ActionResult ProductList(int id,int p=1)
        {
            var productCategory = db.ProductCategorys.Find(id);
            if (productCategory != null)
            {
                var data = productCategory.Products.ToList();
                if (data.Count == 0)
                {
                   // productCategory.Products.Add(new Product() { Name = productCategory.Name + "类别下的商品1", Color = Color.Red, Description = "N\\A", Price = 99, PublishOn = DateTime.Now, ProductCategory = productCategory });
                    //add test data
                    for (int i = 0; i < 10;i++ )
                    {
                        productCategory.Products.Add(new Product() { Name = productCategory.Name + "类别下的商品"+i.ToString(), Color = Color.Blue, Description = "N\\A", Price = 150, PublishOn = DateTime.Now, ProductCategory = productCategory });
                    }
                    db.SaveChanges();
                    data = productCategory.Products.ToList();
                }
                var pagedData = data.ToPagedList(pageNumber: p, pageSize: 2);
                return View(pagedData);
            }
            else
                return HttpNotFound();
        }

        //商品明细
        public ActionResult ProductDetail(int id)
        {
            var data = db.Products.Find(id);
            return View(data);

            // return View();
            //var productCategory = new ProductCategory() { Id = 1, Name = "文具" };
            //var data = new Product()
            //    {
            //        Id = 1,
            //        ProductCategory = productCategory,
            //        Name = "原子笔",
            //        Description = "N/A",
            //        Price = 30,
            //        PublishOn = DateTime.Now,
            //        Color = Color.Black
            //    };

            //return View(data);
        }
    }
}
